package com.companyname.projecttemplate.controller;

import java.util.Arrays;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.base.core.context.annotation.AuthenticationCheck;
import com.base.core.mvc.web.BaseWebController;
import com.base.service.system.dto.JwtUserDTO;
import com.base.service.system.service.UserService;
import com.base.service.system.utils.AuthUtils;
import com.gitee.magic.framework.base.constant.Config;

/**
 * @author start
 */
@RestController
public class MainController extends BaseWebController {

	@Autowired
	private AuthUtils authUtils;
	@Autowired
	private UserService userService;

	@GetMapping("/main/index")
	@AuthenticationCheck
	public ModelAndView index() {
		ModelAndView view=new ModelAndView();
		if(Config.isDevActive()) {
			view.addObject("auths",authUtils.allMenus());
		}else {
			JwtUserDTO user = requestCache(JwtUserDTO.class);
			view.addObject("auths",userService.userAuthTree(0L,user.getUser().getId()));
		}
		addRequestAttribute(view);
		return response(view);
	}
	
	@RequestMapping("/pp/{vp}")
	public ModelAndView page(@PathVariable String vp) {
		ModelAndView view=new ModelAndView();
		Map<String,String[]> p=request.getParameterMap();
		for(String k:p.keySet()) {
			if(p.get(k).length==1) {
				view.addObject(k,p.get(k)[0]);
			}else if(p.get(k).length>1) {
				view.addObject(k,Arrays.asList(p.get(k)));
			}
		}
		addRequestAttribute(view);
		//TODO:枚举可统一添加至这边
		addRequestAttributeEnum(view);
		view.setViewName(vp.replaceAll("-", "/"));
		return view;
	}
	
}
